<template>
	<view class="container">
		<view class="wechatapp">
			<view class="header">
				<!-- <open-data class="avatar" type="userAvatarUrl"></open-data> -->
				<image class="image"
					:src="storeInfo && storeInfo.image_url ? storeInfo.image_url : '/static/default-avatar.png'">
				</image>
			</view>
		</view>
		<view class="auth-title">申请获取以下权限</view>
		<view class="auth-subtitle">获得你的公开信息（昵称、头像等）</view>
		<view class="login-btn">
			<!-- 获取微信用户信息 -->
			<button class="button btn-normal" @click.stop="getDYuserinfo">抖音用户信息授权登录</button>
		</view>
		<view class="no-login-btn">
			<button class="button btn-normal" @click="handleCancel">暂不登录</button>
		</view>
	</view>
</template>
<script>
	import store from '@/store'
	import {
		isEmpty
	} from '@/utils/util'
	import SettingModel from '@/common/model/Setting'
	import * as douyinApi from '@/api/douyin'
	export default {
		data() {
			return {
				result: '', // 用于存储获取到的内容
				code: '',
			}
		},
		methods: {
			getDYuserinfo() {
				const app = this;
				tt.getUserProfile({
				  success(res) {
				    console.log("getUserProfile 调用成功：", res.userInfo);
					uni.login({
						provider: 'toutiao',
						success: function(loginRes) {
							console.log(loginRes);
							if (loginRes) {
								app.code=loginRes.code;
								const param = {
									code: loginRes.code,
								}
								
								douyinApi.gettoken(param)
									.then(result => {
										//app.onEmitSuccess(result.data)
										console.log(result.data);
										//let userinfo = [...result.data,...res.userInfo];
										app.onAuthSuccess(result.data,res.userInfo);
									}).catch(error => {
									console.error(error);
								  });
							}else{
								tt.showToast({
									icon:'fail',
									title:'获取用户信息失败'
								})  
							}
						},
						fail:function(error){
							tt.showToast({
								icon:'fail',
								title:'登录失败'
							})  
						}
						
					});
				  },
				  fail(res) {
					tt.showToast({
						icon:'fail',
						title:'授权失败'
					})  
				    console.log("getUserProfile 调用失败", res);
				  },
				});
				
			},
			async onAuthSuccess(openInfo,userInfo) {
			  const app = this
			  console.log('获取用户信息：',openInfo,userInfo);
			  // 提交到后端
			  store.dispatch('LoginMpDy', {
			      partyData: {
			        code: app.code,
			        oauth: 'MP-TOUTIAO',
					userInfo: userInfo,
					// {
					// 	nickName:'',
					// 	gender:1,
					// 	avatarUrl:'https://hongdaojia.yifayun.com/static/default-avatar.png',
					// },
					 ...openInfo,
			      },
				   ...openInfo,
			      refereeId: store.getters.refereeId
			    })
			    .then(result => {
					 app.onEmitSuccess(userInfo);
			      // 一键登录成功
			      app.$toast(result.message)
			      // 跳转回原页面
			      setTimeout(() => {
			        app.onNavigateBack()
			      }, 1000)
			    })
			    .catch(err => {
					console.log(err);
			      // const resultData = err.result.data
			      // if (isEmpty(resultData)) {
			      //   app.$toast(err.result.message)
			      // }
			      // 跳转回原页面
			      if (resultData.isBack) {
			        setTimeout(() => app.onNavigateBack(1), 2000)
			      }
			      // 判断还需绑定手机号
			      if (resultData.isBindMobile) {
			        app.onEmitSuccess(userInfo)
			      }
			    })
			},
			// 将oauth提交给父级
			// 这里要重新获取code, 因为上一次获取的code不能复用(会报错)
			onEmitSuccess(userInfo) {
				const app = this;
				app.$emit('success', {
					oauth: 'MP-TOUTIAO', // 第三方登录类型: MP-WEIXIN
					code: '', // 微信登录的code, 用于换取openid
					userInfo // 微信用户信息
				})
			},
			/**
			 * 暂不登录
			 */
			handleCancel() {
				// 跳转回原页面
				this.onNavigateBack()
			},

			/**
			 * 登录成功-跳转回原页面
			 */
			onNavigateBack(delta = 1) {
				const pages = getCurrentPages()
				if (pages.length > 1) {
					uni.navigateBack({
						delta: Number(delta || 1)
					})
				} else {
					this.$navTo('pages/index/index')
				}
			}
		},
	}
</script>

<style lang="scss" scoped>
	.container {
		padding: 0 60rpx;
		font-size: 32rpx;
		background: #fff;
		min-height: 100vh;
	}

	.wechatapp {
		padding: 80rpx 0 48rpx;
		border-bottom: 1rpx solid #e3e3e3;
		margin-bottom: 72rpx;
		text-align: center;

		.header {
			width: 190rpx;
			height: 190rpx;
			border: 4rpx solid #fff;
			margin: 0 auto 0;
			border-radius: 50%;
			overflow: hidden;
			box-shadow: 2rpx 0 10rpx rgba(50, 50, 50, 0.3);

			.image {
				display: block;
				width: 100%;
				height: 100%;
			}
		}
	}

	.auth-title {
		color: #585858;
		font-size: 34rpx;
		margin-bottom: 40rpx;
	}

	.auth-subtitle {
		color: #888;
		margin-bottom: 88rpx;
		font-size: 28rpx;
	}

	.login-btn {
		padding: 0 20rpx;

		.button {
			height: 88rpx;
			background: #04be01;
			color: #fff;
			font-size: 30rpx;
			border-radius: 999rpx;
			display: flex;
			justify-content: center;
			align-items: center;
		}
	}


	.no-login-btn {
		margin-top: 20rpx;
		padding: 0 20rpx;

		.button {
			height: 88rpx;
			background: #dfdfdf;
			color: #fff;
			font-size: 30rpx;
			border-radius: 999rpx;
			display: flex;
			justify-content: center;
			align-items: center;
		}
	}
</style>